package cn.itsource.service.blog.imp;




import cn.itsource.blog.domain.param.Param;
import cn.itsource.blog.query.param.ParamQuery;
import cn.itsource.mapper.blog.ParamMapper;
import cn.itsource.service.blog.IParamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author TianXiong on 2023/11/18 10:18
 * &#064;description blog
 */
@Service
@Transactional(readOnly = true,propagation= Propagation.SUPPORTS)
public class ParamServiceImpl implements IParamService {

    @Autowired
    private ParamMapper paramMapper;


    @Override
    public List<Param> getAll() {
        return paramMapper.getAll();
    }



    /**
     * 获取所有的博客参数信息,
     * 将param_key作为key,param_value作为value存在map中
     * @return
     */
    @Override
    public Map<String, Object> getParams() {
        List<Param> all = paramMapper.getAll();
        return all.stream().collect(Collectors.toMap(Param::getParamKey,Param::getParamValue));
    }


    @Transactional
    @Override
    public Param getOne(Long id) {
        return paramMapper.getOne(id);
    }

    @Transactional
    @Override
    public void updateParam(Param param) {
        //mysql中更新article
        param.setUpdateTime(new Date());
        paramMapper.updateParam(param);
    }

    @Transactional
    @Override
    public void insertParam(Param param) {
        paramMapper.insertParam(param);
    }

    @Transactional
    @Override
    public void deleteParam(Long id) {
        paramMapper.deleteParam(id);
    }

    @Override
    public void pathDelete(Long[] ids) {
        paramMapper.pathDelete(ids);
    }

    @Override
    public PageInfo<Param> queryPge(ParamQuery paramQuery) {
        //1.开启分页
        PageHelper.startPage(paramQuery.getCurrentPage(),paramQuery.getPageSize());
        //a.查询总数量 - count()
        //b.会根据方言mysql自动实现分页查询,如果是mysql就会使用limit查询
        //c.自动计算limit的第一个参数
        List<Param> data=paramMapper.queryPge(paramQuery);

        return new PageInfo<>(data);
    }

}

